import { Body, Controller ,Get, Param, Post } from '@nestjs/common';
import { PrismaClient } from '@prisma/client'
import { AuthService } from 'src/jwt/auth.service';

const prisma = new PrismaClient()

@Controller('users')
export class UsersController {
   private readonly authService: AuthService;

        // 用户登录

        @Post('getoneuser')
        async getoneuser(@Body() userInfo) {
           const user= this.authService.validateUser(userInfo)
           return user;
          }
           
      // 查询
    @Get('getUserList')
    async allUsers() {
        const allUsers = await prisma.userInfo.findMany();
        console.log(allUsers);
        return allUsers;
      }

      // 修改
      @Post('updateuser/:user_id')
      async updateuser(@Param('user_id') user_id:number) {
          const updataUser = await prisma.userInfo.update({
            where:{
              id:Number(user_id)
            },
            data:{
              uname:'hures'
            }
          })
          console.log(updataUser);
          return updataUser;
        }

      // 修改
      @Post('insertUser')
      async insertUser() {
          const insertUser = await prisma.userInfo.createMany({
            data:[
             {
              id:2,
              sex:1,   
              age:2,      
              pwd:'1',   
              uname:'test',   
              create_time:'2015-05-15',  
              update_time:'2015-05-15'   
             }
            ],
            skipDuplicates: false, // 插入重复数据时，报错
          })

          console.log(insertUser);
          return insertUser;
      }

      // 删除
      @Post('deletUser/:user_id')
      async deletUser(@Param('user_id') user_id:number) {
          const deletUser = await prisma.userInfo.delete({
            where:{
              id:Number(user_id)
            }
          })
          console.log(deletUser);
          return deletUser;
        }
        
}

